#include "FoodSearchState.h"
#include "ApproachFoodState.h"
#include "ReturnFoodState.h"
#include "SBWander.h"
#include "SBSeparation.h"
#include "SBFlee.h"

#include "AIAgent.h"
#include "AntHill.h"
#include "AntWorld.h"
#include "FoodSource.h"
#include "ControllablePredator.h"

#include <GL\glut.h>

FoodSearchState::FoodSearchState(AIAgent* agent) : AIState(agent)
{
	AntWorld* antWorld = TheAntWorld::Instance(); 
	AntHill* antHill = antWorld->GetAntHill();
	Predator* pred = antWorld->GetRandPredator();
	ControllablePredator* contPred = antWorld->GetControlledPredator();

	m_behaviourMap.insert(BehaviourAndWeight(new SBWander(agent), 0.75f));
	m_behaviourMap.insert(BehaviourAndWeight(new SBSeparation(agent, 3), 1.0f));
	m_behaviourMap.insert(BehaviourAndWeight(new SBFlee(agent, antHill, 7.5f), 0.75f));

	m_behaviourMap.insert(BehaviourAndWeight(new SBFlee(agent, pred, 10.0f), 0.75f));
	m_behaviourMap.insert(BehaviourAndWeight(new SBFlee(agent, contPred, 10.0f), 0.75f));

	m_xMax = 35;
	m_zMax = 35;
}

Vec3f FoodSearchState::Update()
{
	
	//Return to home if strayed to far from home
	Vec3f pos = m_pAgent->GetPos(); 
	if((pos.X > m_xMax) || pos.X < -m_xMax)
	{
		m_pAgent->SetAIState(new ReturnFoodState(m_pAgent));
		return Vec3f();
	}
	else if((pos.Z > m_zMax) || pos.Z < -m_zMax)
	{
		m_pAgent->SetAIState(new ReturnFoodState(m_pAgent));
		return Vec3f();
	}
	
	FoodSource* food = TheAntWorld::Instance()->FoodInRange(m_pAgent->GetPos(), m_pAgent->GetVisionRadius());
	if(food && !(food->IsDepleted()))
	{
		m_pAgent->SetAIState(new ApproachFoodState(m_pAgent, food));
		return Vec3f();
	}
	else
	{
	
		return SumForces();
	}
	

}

const char* FoodSearchState::GetStateName() const 
{
	return "FoodSearchState";
}

FoodSearchState::~FoodSearchState()
{

}